package com.mazaiting.manager.utils;

import com.mazaiting.manager.domain.bo.CustomBrowserBO;
import com.mazaiting.manager.domain.bo.RequestParamBO;
import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;
import eu.bitwalker.useragentutils.Version;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

/**
 * RequestParam 参数实体保存
 *
 * @author 党海亮
 * @create_time 2022/3/26 12:53
 */
public class RequestParamParserUtil {

    /**
     * 日志工具
     */
    private static final Logger logger = LoggerFactory.getLogger(RequestParamParserUtil.class);

    /**
     * 解析request对象内部元素
     * @param request 请求对象
     * @return 请求参数对象
     */
    public static RequestParamBO getParamEntity(HttpServletRequest request){
        String unknown = "Unknown";
        // 获取agent的内容
        // Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36
        // Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36 Edg/99.0.1150.52
        // Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3883.400 QQBrowser/10.8.4559.400
        String userAgent = request.getHeader("user-agent");
        logger.info("userAgent:{}",userAgent);
        // 获取userAgent 对象
        UserAgent userAgentEntity = UserAgent.parseUserAgentString(userAgent);
        // 获取系统版本
        OperatingSystem operatingSystem = userAgentEntity.getOperatingSystem();
        // 获取设备类型
        String deviceInfo = operatingSystem.getDeviceType().getName();
        if (unknown.equals(deviceInfo)){
            // 该出是为找到设备信息,按照目前测试来说.是Computer.接口工具所测试.既默认为Computer
            deviceInfo = "Computer";
        }
        // 获取浏览器对象
        Browser browser = userAgentEntity.getBrowser();
        // 获取浏览器名字
        String browserName = browser.getName();

        Version version;
        String browserVersion;
        if (unknown.equals(browserName)){
            // 为获取到浏览器信息值，则走手动解析userAgent,默认PostMan访问该方法处理有效
            CustomBrowserBO customBrowserBo = parseUserAgent(userAgent);
            // 该出是为找到设备信息,按照目前测试来说.是Windows.接口工具所测试.既默认为Windows
            browserName = customBrowserBo.getBrowser();
            // 浏览器版本信息
            browserVersion = customBrowserBo.getVersion();
        }else{
            // 浏览器版本对象
            version = browser.getVersion(userAgent);
            // 浏览器版本信息
            browserVersion = version.getVersion();
        }

        // 创建返回对象
        RequestParamBO requestParamBo = new RequestParamBO();
        // 设置浏览器以及版本
        requestParamBo.setBrowser(browserName);
        // 获取浏览器版本信息
        requestParamBo.setBrowserVersion(browserVersion);
        // 设置系统过的版本:Windows 10
        requestParamBo.setDeviceInfo(deviceInfo);
        // 设置客户点IP地址
        requestParamBo.setAddrIp(request.getRemoteAddr());
        return requestParamBo;
    }

    /**
     * 在使用接口工具时解析
     * @param userAgent 被解析参数
     * @return 自定义浏览器对象
     */
    private static CustomBrowserBO parseUserAgent(String userAgent){
        String[] split = userAgent.split("/");
        CustomBrowserBO browserBo = new CustomBrowserBO();
        browserBo.setBrowser(split[0]);
        browserBo.setVersion(split[1]);
        return browserBo;
    }
}
